$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'subway/stations/list',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'id', index: 'id', width: 50, key: true, hidden: true },
            { label: '站点ID', name: 'statId', index: 'stat_id', width: 50, hidden: true },
            { label: '站点名', name: 'name', index: 'name', width: 80 },
            { label: '线路名', name: 'lineNames', index: 'line_names', width: 80 },
            // { label: '终点站', name: 'isTerminal', index: 'is_terminal', width: 80 },
            // { label: '最早发车时间', name: 'earliestDepTime', index: 'earliest_dep_time', width: 80 },
            // { label: '最晚发车时间', name: 'latestDepTime', index: 'latest_dep_time', width: 80 },
            { label: '状态', name: 'status', index: 'status', width: 80 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x": "hidden" });
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        stations: {},
        lineName: "选择线路",
        lines: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.stations = {};

            vm.listLines()
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
            vm.listLines()
        },
        saveOrUpdate: function (event) {
            console.log(vm.stations)
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.stations.statId == null ? "subway/stations/save" : "subway/stations/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.stations),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", { icon: 1 });
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "subway/stations/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code === 0) {
                                layer.msg("操作成功", { icon: 1 });
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "subway/stations/info/" + id, function (r) {
                vm.stations = r.stations;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        },
        listLines: function () {
            $.get(baseURL + "subway/lines/list?limit=100", function (r) {
                if (r.code !== 0) {
                    console.error("failed to list lines: %o", r)
                    layer.alert(r.msg);
                    return
                }
                vm.lines = r.page.list
            });
        },
        choseLine: function (line) {
            lines = [
                line.lineId
            ]
            vm.stations.line_ids = lines
            vm.lineName = line.name
        }
    }
});
